﻿using System.Runtime.Serialization;
using SomeKit.Serialization;

namespace SomeKit.Security
{
    [DataContract]
    public class SecureData<T>
    {
        [DataMember]
        public string CipherData { get; set; }

        public string Password { get; set; }
        public string Salt { get; set; }

        public T Data
        {
            get
            {
                return AES.Decrypt(CipherData, Password, Salt).DeserializeXml<T>();
            }
            set
            {
                CipherData = AES.Encrypt(value.SerializeXml(), Password, Salt);
            }
        }

        public T GetData(string password, string salt)
        {
            return AES.Decrypt(CipherData, password, salt).DeserializeXml<T>();
        }
        public void SetData(T data, string password, string salt)
        {
            CipherData = AES.Encrypt(data.SerializeXml(), password, salt);
        }
    }
}
